﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Watson.Core.Tools;
using ITOMMS.Entity;
using ITOMMS.Business;
using System.Text;

namespace ITOMMS.Web.SystemManage
{
    public partial class BackLog : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (SessionHelper.UserRoleGroup == PubEnums.RoleGroup.管理员.ToString())
                {
                    DropDownListHelper.InitDropList<tbUserEntity>(new tbUserEntity().GetAllByQuery("RoleGroup<>" + (int)PubEnums.RoleGroup.客户), this.ddlUserName, "ID", "RealName");//绑定员工姓名
                }
                else
                {
                    DropDownListHelper.InitDropList<tbUserEntity>(new tbUserEntity().GetAllByQuery("RoleGroup<>" + (int)PubEnums.RoleGroup.客户), this.ddlUserName, "ID", "RealName");//绑定员工姓名
                    this.ddlUserName.Items.FindByText(SessionHelper.RealName).Selected = true;
                    this.ddlUserName.Enabled = false;
                }

                DataShow(GetSearchSql());

            
            }
        }

        private void DataShow(string searchSql)
        {
            tbWorkPlanEntity entity = new tbWorkPlanEntity();
            AspNetPager1.RecordCount = entity.GetTotalRecords();
            this.dgdaoim_sus_plyedr.DataSource = entity.GetAllByQuery(searchSql, "order by id desc", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            dgdaoim_sus_plyedr.DataBind();

        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            DataShow(GetSearchSql());
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            DataShow(GetSearchSql());
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("BackLogAdd.aspx", true);
        }

        private string GetSearchSql()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" 1=1 ");
            if (this.ddlUserName.SelectedValue.Trim().Length > 0 && this.ddlUserName.SelectedValue.Trim()!="-1")
            {
                sb.Append(" and  CreateBy=").Append(this.ddlUserName.SelectedValue.Trim());
            }
            if (this.txtDoDate_Bgn.Text.Trim().Length > 0)
            {
                sb.Append(" and  DoDate >= '").Append(this.txtDoDate_Bgn.Text.Trim()).Append("'");
            }
            if (this.txtDoDate_End.Text.Trim().Length > 0)
            {
                sb.Append(" and  DoDate <= '").Append(this.txtDoDate_End.Text.Trim()).Append("'");
            }
            return sb.ToString();
        }

        public string GetStatus(string param)
        {
            string result = "未处理";
            if (param.Trim() == "1")
                result = "处理中";
            else if (param.Trim() == "2")
                result = "已处理";

            return result;
        }

        public string GetUserName(string param)
        {
            string result = "";
            VwUserEntity userEntity = new VwUserEntity();
            DataSet ds = new DataSet();
            ds = userEntity.GetDataSet(" select RealName from tb_user where ID=" + param.ToString());
            if (ds.Tables[0].Rows.Count > 0)
                result = ds.Tables[0].Rows[0]["RealName"].ToString();
            return result;
        }

        protected void dgdaoim_sus_plyedr_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName.ToString() == "del")
                {
                    if (e.CommandArgument.ToString() != "")
                    {

                        if (new tbWorkPlanEntity().Delete(int.Parse(e.CommandArgument.ToString())) > 0)
                        {
                            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.succeedInfo('删除成功。',300,180,'提示',null);</script>");
                            DataShow(GetSearchSql());
                        }
                        else
                        {
                            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.errorInfo('删除失败。',300,180,'提示',null);</script>");
                        }

                    }
                }
            }
            catch (APPException appex)
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.alert('" + appex.Message + "。',300,180,'提示',null);</script>");
            }
            catch (Exception ex)
            {
                log4netHelper.error("", ex);
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.errorInfo('删除失败。',300,180,'提示',null);</script>");
            }
        }

        protected void dgdaoim_sus_plyedr_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowIndex > -1)
            {
                //如果是绑定数据行 
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (e.Row.Cells[1].Text.Length > 20)
                    {
                        e.Row.Cells[1].Text = e.Row.Cells[1].Text.Substring(0, 20);
                    }


                    if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                    {
                        ((LinkButton)e.Row.Cells[6].FindControl("lbtnDelete")).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
                    }
                }
            }
        }
    }
}
